﻿using System;

using Gonte.Caching;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MyUtilitiesTest
{
    /// <summary>
    ///This is a test class for ObjectCacheTest and is intended
    ///to contain all ObjectCacheTest Unit Tests
    ///</summary>
    [TestClass()]
    public class ObjectCacheTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestMethod()]
        public void ObjectCacheGetCacheTest()
        {
            Cache cache = ObjectCache.GetCache(typeof(String));
            Assert.IsNotNull(cache);
            Assert.IsNotNull(cache.ExpirationPolicy);

            cache.ExpirationPolicy = new LruExpirationPolicy(1000);
        }

        //[TestMethod()]
        //public void ObjectCacheMultithreadedTest()
        //{
        //    const int WriterThreadCount = 5;
        //    const int ReaderThreadCount = 5;

        //    // Create the threads

        //    Thread[] writerThreads = new Thread[WriterThreadCount];
        //    Thread[] readerThreads = new Thread[ReaderThreadCount];

        //    for (int i = 0; i < WriterThreadCount; ++i)
        //    {
        //        writerThreads[i] = new Thread(UpdateCache);
        //        writerThreads[i].Name = "WriterThread" + i;
        //    }

        //    for (int i = 0; i < ReaderThreadCount; ++i)
        //    {
        //        readerThreads[i] = new Thread(ReadCache);
        //        readerThreads[i].Name = "ReaderThread" + i;
        //    }

        //    // Start the threads

        //    for (int i = 0; i < WriterThreadCount; ++i)
        //    {
        //        writerThreads[i].Start();
        //        writerThreads[i].Join();
        //    }

        //    for (int i = 0; i < ReaderThreadCount; ++i)
        //    {
        //        readerThreads[i].Start();
        //        readerThreads[i].Join();
        //    }

        //}

        //public void UpdateCache()
        //{
        //    String threadName = Thread.CurrentThread.Name;

        //    Console.WriteLine("Setting cache: " + threadName);

        //    ObjectCache.GetCache(typeof(String))["1"] = threadName;
        //}

        //public void ReadCache()
        //{
        //    String threadName = Thread.CurrentThread.Name;

        //    Console.WriteLine("Reading cache: " + ObjectCache.GetCache(typeof(String))["1"]);
        //}
    }
}
